
//
//  FZBlogTextView.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/5.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

class FZBlogTextView: UITextView {

    var textFont : CGFloat = 0 {
        
        didSet{
            
            font = UIFont.systemFont(ofSize: textFont)
            placeHolder.font = font
        }
    }
    
    var textString : String? {
        
        didSet {
            
            text = textString
            
        }
    }
    var placeHodlerString : String? {
        
        didSet {
            
            placeHolder.text = placeHodlerString
            
        }
    }
    
    fileprivate lazy var placeHolder : UILabel = {
    
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 20)
        lab.textColor = UIColor.lightGray
        lab.numberOfLines = 0
        return lab
    }()
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        NotificationCenter.default.addObserver(self, selector: #selector(deleteTextView), name: NSNotification.Name(deleteTextViewNoti), object: nil)
        
     }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   fileprivate func setupUI() {
        addSubview(placeHolder)
        placeHolder.snp.makeConstraints { (make) in
        make.left.top.equalTo(self).offset(5)
        make.width.equalTo(self).offset(-10)
        make.bottom.equalTo(self)
     }
    NotificationCenter.default.addObserver(self, selector: #selector(textViewValueChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
            
   }
   
    @objc private func textViewValueChange() {
        
        placeHolder.isHidden = self.hasText
    }

    @objc private func deleteTextView() {
        
        self.deleteBackward()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }


}




















